using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;

public class TestJs : MonoBehaviour
{
    #if UNITY_WEBGL && !UNITY_EDITOR
    [System.Runtime.InteropServices.DllImport("__Internal")]
    private static extern int TestJsInt(int a);

    [System.Runtime.InteropServices.DllImport("__Internal")]
    private static extern string TestJsString(string str);

    [System.Runtime.InteropServices.DllImport("__Internal")]
    private static extern void TestJsPrintFloatArray(float[] arr, int size);

     [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
     public delegate void CallBackType(int a, string b);

    [System.Runtime.InteropServices.DllImport("__Internal")]
    private static extern void TestJsCallback(CallBackType callback, int a, string b);

    void Start()
    {   
        int a = TestJsInt(456123);
        Debug.LogError("test js return int: " + a);

        string b = TestJsString("this is c# string");
        Debug.LogError("test js return string: " + b);

        float[] farr = new float[]{1.5f, 2.3f, 3.4f};
        TestJsPrintFloatArray(farr, farr.Length);

        TestJsCallback(CallbackFromJs, 234, "a c# string");
    }


     
     [AOT.MonoPInvokeCallback(typeof(CallBackType))]
    static void CallbackFromJs(int a, string b)
    {
        Debug.LogError("callbacked from js: " + a + " | " + b);
    }
    #else
    void Start()
    {

    }
    #endif
}
